package com.function.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.function.domin.dto.BucketDTO;
import com.function.domin.dto.DownloadDTO;
import com.function.domin.pojo.Bucket;
import com.function.domin.pojo.Download;
import com.function.domin.pojo.Upload;
import com.function.mapper.BucketMapper;
import com.function.mapper.DownloadMapper;
import com.function.properties.AliyunOSSProperties;
import com.function.service.OSSService;
import com.function.mapper.UploadMapper;
import com.function.utils.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;

/**
* @author WiedersehenM
* @description 针对表【upload(上传文件记录)】的数据库操作Service实现
* @createDate 2024-07-03 11:54:05
*/
@Service
public class OSSServiceImpl extends ServiceImpl<UploadMapper, Upload> implements OSSService {

    @Autowired
    private UploadMapper uploadMapper;
    @Autowired
    private BucketMapper bucketMapper;
    @Autowired
    private DownloadMapper downloadMapper;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    /**
     * 创建bucket
     * @param bucketDTO
     */
    @Transactional
    public void createBucket(BucketDTO bucketDTO) {
        //调用createBucket方法创建bucket
        aliOSSUtils.createBucket(bucketDTO.getBucketName());
        //记录至数据库
        Bucket bucket = Bucket.builder()
                .bucketName(bucketDTO.getBucketName())
                .createTime(LocalDateTime.now())
                .build();
        bucketMapper.insert(bucket);
    }

    /**
     * 删除bucket
     * @param bucketDTO
     */
    @Transactional
    public void deleteBucket(BucketDTO bucketDTO) {
        //调用deleteBucket方法删除bucket
        aliOSSUtils.deleteBucket(bucketDTO.getBucketName());
        //记录至数据库
        HashMap<String, Object> bucketMap = new HashMap<>();
        bucketMap.put("bucket_name", bucketDTO.getBucketName());
        bucketMapper.deleteByMap(bucketMap);
    }

    /**
     * 上传文件至阿里云oss
     * @param image
     * @return
     */
    @Transactional
    public String upload(MultipartFile image) throws IOException {
        //调用aliOSS工具包实现上传
        String url = aliOSSUtils.upload(image);
        //记录至数据库
        Upload upload = Upload.builder()
                .url(url)
                .createTime(LocalDateTime.now())
                .bucketName(aliyunOSSProperties.getBucketName())
                .build();
        uploadMapper.insert(upload);
        return url;
    }

    /**
     * 从oss下载文件至本地
     * @param downloadDTO
     * @return
     */
    @Transactional
    public String download(DownloadDTO downloadDTO) {
        //调用aliOSS工具包实现下载
        String url = aliOSSUtils.download(downloadDTO);
        //记录至数据库
        Download download = Download.builder()
                .bucketName(downloadDTO.getBucketName())
                .objectName(downloadDTO.getObjectName())
                .url(url)
                .createTime(LocalDateTime.now())
                .build();
        downloadMapper.insert(download);
        return url;
    }
}




